package com.geping.etl.common.controller;

import com.alibaba.fastjson.JSONObject;
import com.geping.etl.common.entity.Sys_Menu;
import com.geping.etl.common.entity.Sys_UserAndOrgDepartment;
import com.geping.etl.common.service.SysUserReportService;
import com.geping.etl.common.service.Sys_MenuService;
import com.geping.etl.common.util.VariableUtils;
import com.geping.etl.utils.EnvUitl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: David
 * @Date: Created in 9:55 2020/4/22
 * @Description: 公共导入导出服务控制器
 * @Modified By:
 * @Version: 0.0.1*/


@RestController
public class ImpExpMarController {

    private String serverUrl;

    private String getServerUrl() {
        return EnvUitl.props.getProperty("commomServer.host") + "/" + EnvUitl.props.getProperty("commomServer.serverName");
    }

    @Autowired
    private Sys_MenuService sys_menuService;

    @Autowired
    private SysUserReportService sysUserReportService;

    @GetMapping("/dataCount")
    public ModelAndView dataCount(String panelId, HttpServletRequest request) {

        Sys_UserAndOrgDepartment sysUser = (Sys_UserAndOrgDepartment)request.getSession().getAttribute("sys_User");
        VariableUtils vu = (VariableUtils) request.getSession().getAttribute("vu");
        String id = sysUser.getLoginid();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("common/dataCount");
        modelAndView.addObject("panelId", panelId);
        modelAndView.addObject("loginId", id);
        modelAndView.addObject("serverUrl", this.getServerUrl());
        modelAndView.addObject("authReports", JSONObject.toJSONString(sysUserReportService.queryUserReportById(request, sysUser.getId())));
        return modelAndView;
    }

    @GetMapping("/dataMarView")
    public ModelAndView dataMarView(@RequestParam("tableId") String tableId, HttpServletRequest request) {
        Sys_UserAndOrgDepartment sysUser = (Sys_UserAndOrgDepartment)request.getSession().getAttribute("sys_User");
        String id = sysUser.getLoginid();
        String panelId = request.getParameter("panelId");
        String dataStatus = request.getParameter("dataStatus");
        String name = request.getParameter("name");
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("common/dataMar");
        modelAndView.addObject("panelId", panelId);
        modelAndView.addObject("dataStatus", dataStatus);
        modelAndView.addObject("tableId", tableId);
        modelAndView.addObject("name", name);
        modelAndView.addObject("loginId", id);
        modelAndView.addObject("serverUrl", this.getServerUrl());
        return modelAndView;
    }


    @GetMapping("/tableMarView")
    public ModelAndView tableMarView(HttpServletRequest request) {
        Sys_UserAndOrgDepartment sysUser = (Sys_UserAndOrgDepartment)request.getSession().getAttribute("sys_User");
        String id = sysUser.getLoginid();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("common/tableMar");
        modelAndView.addObject("loginId", id);
        modelAndView.addObject("serverUrl", this.getServerUrl());
        return modelAndView;
    }

/**
     * 看板管理
     * @return*/
    @GetMapping("/panelView")
    public ModelAndView panelView(HttpServletRequest request) {
        Sys_UserAndOrgDepartment sysUser = (Sys_UserAndOrgDepartment)request.getSession().getAttribute("sys_User");
        String id = sysUser.getLoginid();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("loginId", id);
        modelAndView.addObject("serverUrl", this.getServerUrl());
        modelAndView.setViewName("common/panelMag");
        return modelAndView;
    }

    @GetMapping("/checkLogView")
    public ModelAndView checkLogView() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("common/checkLog");
        modelAndView.addObject("serverUrl", this.getServerUrl());
        return modelAndView;
    }



    @PostMapping("/addMenu")
    public Map<String, String> addMenu(HttpServletRequest request, HttpServletResponse response) {
        Map<String, String> responseJson = new HashMap<>();
        try {
            request.setCharacterEncoding("UTF-8");
            String viewName = request.getParameter("viewName");
            String subject = request.getParameter("subject");
            String resource = request.getParameter("resource");
            String tableSerial = request.getParameter("tableSerial");
            String url = request.getParameter("url");
            if (!url.equals("=")){
                url = url.substring(0,url.indexOf("?")+8)+"="+url.substring(url.indexOf("?")+8);
            }
            if (StringUtils.isBlank(viewName) || StringUtils.isBlank(subject)
                    || StringUtils.isBlank(resource) || StringUtils.isBlank(tableSerial)) {
                responseJson.put("msg", "必填项不能为空!");
            }
            //查询最大id
            Sys_Menu sys_menu = sys_menuService.findMax();
            Sys_Menu saveMenu = new Sys_Menu();
            int id = Integer.valueOf(sys_menu.getId());
            saveMenu.setId(++id);
            saveMenu.setName(viewName);
            saveMenu.setSubjectId(subject);
            saveMenu.setPmenuid(resource);
            saveMenu.setTarget("mainFramePage");
            saveMenu.setOrderNum((Integer.parseInt(sys_menu.getOrderNum()) + 1) + "");
            saveMenu.setUrl(url);
            //插入数据库
            Sys_Menu sys_menu1 = sys_menuService.save(saveMenu);
            if (sys_menu1 != null) {
                responseJson.put("code", "000000");
                responseJson.put("menuId", String.valueOf(saveMenu.getId()));
            }
        } catch (Exception e) {
            e.printStackTrace();
            responseJson.put("msg", "处理失败!");
        }
        return responseJson;
    }





}
